package com.lrsEducation.controller;

import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lrsEducation.Vo.Registeruser;
import com.lrsEducation.entity.UcenterMember;
import com.lrsEducation.service.IUcenterMemberService;
import com.lrsEducation.utils.R;
import com.lrsEducation.utils.SendMs;
import com.lrsEducation.utils.loginUtil;
import org.jose4j.lang.JoseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Mr.tang
 * @since 2022-05-19
 */
@CrossOrigin
@RestController
@RequestMapping("/ucenterMember")
public class UcenterMemberController {

    @Autowired
    private IUcenterMemberService ucenterMemberService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 用户注册Controller
     * */
    @PostMapping("userRegister")
    public R userRegister(@RequestBody Registeruser registerUser){
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",registerUser.getMobile());
        long count = ucenterMemberService.count(queryWrapper);
        if(count > 1){
            ucenterMemberService.saveUser(registerUser);
            return R.ok().msg("用户注册成功！");
        }
        else{
            return R.ok().msg("此账号已存在！");
        }

    }

    /**
     * 用户登录Controller
     * */
    @PostMapping ("userLogin")
    public R userLogin(@RequestBody UcenterMember ucenterMember) {
        boolean isLogin = ucenterMemberService.userLogin(ucenterMember);
        if (isLogin){
            QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mobile",ucenterMember.getMobile());
            UcenterMember one = ucenterMemberService.getOne(queryWrapper);
            String token = loginUtil.createToken(one.getId(), one.getMobile());
            return R.ok().msg("登录成功！").data("token",token);
        }else {
            return R.error().msg("登陆失败!");
        }

    }


    /**
     * 发送验证码
     * */
    @GetMapping("sendMsm/{phone}")
    public R sendMsm(@PathVariable String phone){
        System.out.println(phone);
        String verification = redisTemplate.opsForValue().get(phone);
        System.out.println(verification);
        if(!StringUtils.isEmpty(verification)){
            return R.ok().msg("");
        }
        String verificationToRedis = SendMs.sendMs(phone);
        redisTemplate.opsForValue().set(phone,verificationToRedis,5, TimeUnit.MINUTES);
        return R.ok().msg("发送成功！").data("verification",verificationToRedis);
    }

    /**
     * 根据token查询用户信息
     * */
    @GetMapping ("/getMesByToken/{token}")
    public R getMesByToken(@PathVariable("token") String token){
        JWT jwt = JWT.of(token);
       String id = (String) jwt.getPayload("id");
        UcenterMember member = ucenterMemberService.getById(id);
        return R.ok().data("member",member);
    }


    /**
     * 统计注册人数
     * */
    @GetMapping("/getRegisterCount/{day}")
    public R getRegisterCount(@PathVariable("day") String date){
        int registerCount = ucenterMemberService.getRegisterCount(date);
        return R.ok().data("registerCount",registerCount);
    }

}
